<?php
/*************************************************************************
Module contact
----------------------------------------------------------------
Avasoft CMS Project
Company: Avasoft Co., Ltd                                  
Name: Tran Thi Kim Que                                   
Last updated: 08/03/2011
**************************************************************************/

ini_set('display_errors','1'); ini_set('display_startup_errors','1'); 
include_once(ROOT_PATH."includes/functions.php");
include_once(ROOT_PATH."plugins/authimg/authimg.php");

include_once(ROOT_PATH."classes/dao/systems.class.php");

include_once("class.phpmailer.php");
include_once("class.smtp.php");
$templateFile = "contactform.tpl.html";
 $slug = $request->element('slug','');
$template->assign("slug",$slug);

# contact form
$full_name = $request->element('first_name','');
$chucdanh = $request->element('chucdanh','');
$email = strtolower($request->element('email',''));
$cty = $request->element('cty','');
$address = $request->element('address','');
$tel = $request->element('tel','');
$fax = $request->element('fax','');
$province = $request->element('province','');
$country = $request->element('country','');
$postcode = $request->element('postcode','');
$message = $request->element('message','');
$title = $request->element('title','');
$admin_mail = $siteConfigs->getProperty('admin_mail');
$company = $siteConfigs->getProperty('company');
$website = $siteConfigs->getProperty('website');
$template->assign("fax",$fax);
$template->assign("admin_mail",$admin_mail);
$pageName = 'Liên hệ';
$template->assign("pageName",$pageName);
if($_POST) {
	include_once(ROOT_PATH."classes/data/validate1.class.php");
	$listerror = validateData($request);
	$check = checkvalidate($listerror);
	if($check == 0)	{
		$response ="Chào bạn, bạn nhận được một email liên hệ từ website ".$website." với nội dung như sau: <br/>";
		$response .="Họ tên: $full_name"."<br/>";
		$response .="Địa chỉ: $address"."<br/>";
		$response .="Điện thoại: $tel"."<br/>";
		$response .="Email: $email"."<br/><br/>";
		$response .="Nội dung liên hệ: $message"."<br/>";
		//mail("info@mypapit.net","Contact form fakapster",$response, $headers);
		$mail = new PHPMailer();
		$mail->IsSMTP(); // set mailer to use SMTP
		$mail->Host =SMTP_HOST; // specify main and backup server
		$mail->Port = SMTP_PORT; // set the port to use
		$mail->SMTPAuth = true; // turn on SMTP authentication
		#$mail->SMTPSecure = 'ssl';
		$mail->Username = SMTP_USER; // your SMTP username or your gmail username
		$mail->Password = SMTP_PASSWORD; // your SMTP password or your gmail password
		$from = SMTP_USER; // Reply to this email
		$to=$admin_mail; // Recipients email ID
		$name=$company; // Recipient's name
		$mail->header = 'Content-Type: text/html; charset=utf-8'."\r\n";
		$mail->From = $from;
		$mail->FromName = $full_name; // Name to indicate where the email came from when the recepient received
		$mail->AddAddress($admin_mail,$admin_mail);
		$mail->AddReplyTo($email,$full_name);
		$mail->WordWrap = 50; // set word wrap
		$mail->IsHTML(true); // send as HTML
		$mail->Subject =$title;
		$mail->Body = $response; //HTML Body
		//$mail->AltBody = "Mail nay duoc goi bang phpmailer class. - bloghoctap.com"; //Text Body
		//$mail->SMTPDebug = 2;
		
		if(!$mail->Send())
		{
			$error = $messages['error_ok'].'<br />';
		}
		else
		{
			$error = $messages['contact_ok'].'<br />';
			$template->assign("error",$error);
		}
	}else{
		$template->assign("listerror",$listerror);
	}
}
if(isset($_SESSION['rand_code'])){
	unset($_SESSION['rand_code']);	
}
function checkvalidate($validate){
	foreach($validate as $check){
		if($check != NULL)
			return 1;
	}
	return 0;
}
function validateData($request) {
	global $messages;
	$error = array();
	$validate = new Validate();
	$code = strtolower($request->element('code'));
	$error['3']= $validate->validString($messages['security'],$request->element('code'));	
	if(!isset($_SESSION['rand_code']) || $code != strtolower($_SESSION['rand_code'])) $error['3']= $messages["invalid_security_code"].'<br />';
	return $error;
}

 // dung chung
 include_once(ROOT_PATH."modules/framework.module.php");
 //page active
  $active='lien-he';
 $template->assign('active',$active);
?>